<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
  <meta name="referrer" content="never">
  <title>匀速动画</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    ul {
      list-style: none;
    }

    .box{
      position: absolute;
      top: 50px;
      left: 50px;
      width: 150px;
      height: 150px;
      background-color: #f00;
    }
  </style>
</head>
<body>
  <div class="box"></div>

  <script type="text/javascript">
    var box = document.querySelector(".box");  

    var flag = false;
    box.onclick = function (){
      move(this, 13, (flag = !flag) ? 600 : 50);
    }

    function move(ele, step, end){
      step = ele.offsetLeft > end ? -step : step;

      // 动画帧数 -> 1s展示图片的个数   60帧 -> 90 120 144 165Hz
      var timer = setInterval(function (){
        // 当下元素与页面的左边距  this.offsetLeft
        ele.style.left = ele.offsetLeft + step + "px";
        
        // 运动到600 清除定时器
        if(Math.abs(end - ele.offsetLeft) < Math.abs(step)){
          // 如果超出，变回来
          ele.style.left = end + "px";
          clearInterval(timer);
        }
      }, 1000/60)
    }
  </script>
</body>
</html>